﻿package app.model.proxy {	
	
    import app.facade.App;    
	import app.facade.EApp;
	import app.model.vo.TransactionVO;
	import app.view.component.moment.Module;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import com.adobe.serialization.json.JSON;
	import com.hutra.utils.ArrayUtilities;
	import flash.system.Security;
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
	import org.osflash.thunderbolt.Logger;
		
	/**
	 * @author hungconcon
	 */
    public class ConnectProxy extends Proxy implements IProxy
    {
		
		/**
		 * 
		 * @param	command
		 * @param	object
		 * @param	tcode
		 * @param	sysn
		 */
		public function Spin(command:String, object:Object = null, tcode:Number = NaN, attach:*= null, sysn:*= null):void {
			//var spinVo:SpinVO = object as SpinVO;
			//var url:String = "request/bet/" + spinVo.bet + "/" +spinVo.line + "/dsadas.htm"; 
			//onPostdata(url, command, object, tcode, attach, sysn);
		}
		

		/**************************************************************
		 * 															  *
		 * 					PLEASE, DO NOT CHANGE!					  *
		 * 															  *
		 * ************************************************************/
		
		/**
		 * Creates a new URLVariables object. You pass URLVariables
		 * objects to the data property of URLRequest objects.
		 * 
		 * If you call the URLVariables constructor with a string, 
		 * the decode() method is automatically called
		 * to convert the string to properties of the URLVariables object.
		 */
		private var variables:URLVariables;
		
		/**
		 * Always default exists to determine the object reference
		 */
		public static const NAME:String = "ConnectProxy";	
		
		/**
		 *  Creates a UtilityProxy object.
		 */
		private var connect:UtilityProxy = new UtilityProxy();
		
		/**
		 * Creates a ConnectProxy object.
		 */
		public function ConnectProxy() {
			super(NAME);
        }
		
		/**
		 * Called by the Model when the Proxy is registered
		 */
		override public function onRegister():void {
			super.onRegister();
		}
		
		/**
		 * 
		 * @param	cmd
		 * @param	command
		 * @param	object
		 * @param	code
		 * @param	sysn
		 */
		private function onPostdata(cmd:String, command:String, object:Object = null, tcode:Number=NaN, attach:*=null, sysn:*= null):void {
			var rollback:TransactionVO = new TransactionVO( object, tcode);
				rollback.attach = attach;
			variables = parserVariable(cmd, rollback);
			onConnnect(command, variables, rollback, sysn);
		}
		
		/**
		 * Connect and loads data from the specified URL. The data can be received as
		 * text, depending on the variable you set for the URLVariables property. Note that
		 * the default value of the URLVariables property is text.
		 * If you want to send data to the specified URL, you can set the data
		 * property in the URLRequest object.
		 * 
		 * @param	variable
		 * @param	command
		 * @param	rollback
		 */
		protected function onConnnect(command:String, variable:URLVariables, rollback:*= null, sysn:*= null):void {
			//[0]
			Module.stage.removeEventListener(command, onNotification);
			Module.stage.addEventListener(command, onNotification);
			//[1]
			connect.load(command, App.URL["u.1"], variable, rollback, sysn);
		}
		
		/**
		 * Creates a new URLVariables object. You pass URLVariables objects to the data property of URLRequest objects.
		 * @param	command
		 * @param	trans
		 * @return
		 */
		private function parserVariable(command:String,trans:TransactionVO):URLVariables {
			var a:URLVariables = new URLVariables();
				a.cmd = command;
			for (var p:* in trans.post) 
				a[p] = trans.post[p];
			return a;
		}
		
		/**
		 * Moving beyond data
		 * @param	evt
		 */
		protected function onNotification(evt:Event):void {
			sendNotification(evt.type, evt.currentTarget);
		}
    }
}